import { http } from '../http/axios';
import { ENDPOINTS } from '../constants/endpoints';
import type {
  BorrowRecord,
  CreateBorrowRequest,
  UpdateBorrowRequest,
  BorrowQueryParams,
  ReturnBorrowRequest
} from '../types/borrow';
import type { HealthCheckResponse } from '../types/common';

export const borrowService = {
  // 健康检查
  healthCheck: async (): Promise<HealthCheckResponse> => {
    const response = await http.get('/');
    return response.data;
  },

  // 获取借用记录列表
  getBorrows: async (params?: BorrowQueryParams): Promise<BorrowRecord[]> => {
    const response = await http.get(ENDPOINTS.BORROW.LIST, { params });
    return response.data;
  },

  // 获取单个借用记录
  getBorrow: async (id: number): Promise<BorrowRecord> => {
    const response = await http.get(ENDPOINTS.BORROW.DETAIL(id));
    return response.data;
  },

  // 创建借用记录
  createBorrow: async (data: CreateBorrowRequest): Promise<BorrowRecord> => {
    const response = await http.post(ENDPOINTS.BORROW.CREATE, data);
    return response.data;
  },

  // 更新借用记录
  updateBorrow: async (id: number, data: UpdateBorrowRequest): Promise<BorrowRecord> => {
    const response = await http.put(ENDPOINTS.BORROW.UPDATE(id), data);
    return response.data;
  },

  // 删除借用记录
  deleteBorrow: async (id: number): Promise<void> => {
    await http.delete(ENDPOINTS.BORROW.DELETE(id));
  },

  // 归还物资
  returnBorrow: async (id: number, data?: ReturnBorrowRequest): Promise<BorrowRecord> => {
    const response = await http.post(ENDPOINTS.BORROW.RETURN(id), data);
    return response.data;
  },
};